;; import lists/member.scm

(define (opener? b)
  (member b '(#\( #\[ #\{)))

(define (matching? a b)
  (member (list a b)
	  '((#\( #\))
	    (#\[ #\])
	    (#\{ #\}))))

(define (parse l)
  (let loop ((stack '()) (l (string->list l)))
    (if (null? l)
	(if (null? stack)
	    "Good"
	    "unclosed bracket")
	(let ((bracket (car l)))
	  (if (opener? bracket)
	      (loop (cons bracket stack) (cdr l))
	      (if (null? stack)
		  "too many close brackets"
		  (let ((top (car stack)))
		    (if (matching? top bracket)
			(loop (cdr stack) (cdr l))
			"bracket mismatch"))))))))

(define (go t)
  (write-line `(parsing ,t))
  (print (parse t))
  (newline))

(go "")
(go "(")
(go ")")
(go "()")
(go "(]")
(go "[[]]")
(go "(()[])")
(go "(((()))[][]{{}{}})")
